#include "StdAfx.h"
#include "ImproMsgCon.h"
#include "Util/UtilInclude.h"
#include "Workout.h"




CImproMsgCon::CImproMsgCon(HWND hWnd)
:CImproveCon( hWnd )
{
	m_nIndexCon = IMSGCON_OTHER;
}

CImproMsgCon::~CImproMsgCon(void)
{
}


BOOL CImproMsgCon::AlphaBlendMaskBk( CDC *pDCBK, HBITMAP hbmpBK,int nalpha )
{
	CDC dcBK;
	dcBK.CreateCompatibleDC( pDCBK->m_hDC );
	HBITMAP hBmpBKOld = dcBK.SelectBitmap( hbmpBK );
	BLENDFUNCTION blender;
	blender.BlendOp = AC_SRC_OVER;
	blender.BlendFlags = 0;
	blender.SourceConstantAlpha = nalpha;
	blender.AlphaFormat   =  0;

	BOOL bRet = ::AlphaBlend( pDCBK->m_hDC,m_rcMsg.left,m_rcMsg.top,m_rcMsg.Width(),m_rcMsg.Height(),
		dcBK.m_hDC,0,0,m_rcMsg.Width(),m_rcMsg.Height(),blender);

	if ( NULL!=hBmpBKOld )
		dcBK.SelectBitmap( hBmpBKOld );
	dcBK.DeleteDC();
	return TRUE;
}

void CImproMsgCon::ShowAnimate( CDC* pDCFront,  CDC* pDCBK,BOOL bShowToFont  )
{
	//draw maskani alpha

	int  nalpha = 180;
	AlphaBlendMaskBk( pDCBK, GetBackGnd(), nalpha );


	//create DIBSection bk 
	CRect rcBK (0,0,CUtil::GetScreenWidth(), CUtil::GetScreenHeight() );

	CDC memDIBDC;
	memDIBDC.CreateCompatibleDC( pDCFront->m_hDC ); 
	HANDLE hBKDBInfo = GlobalAlloc(GHND,sizeof(BITMAPINFO) + sizeof(DWORD)*3 ); //+ CFastAlphaBmp::Get16BppScreenSize() 
	BITMAPINFO * lpBKBmpInfo=(BITMAPINFO *)GlobalLock(hBKDBInfo);
	ZeroMemory( lpBKBmpInfo , sizeof(BITMAPINFO) + sizeof(DWORD)*3 ); 
	lpBKBmpInfo->bmiHeader.biSize		=  sizeof(BITMAPINFOHEADER); 
	lpBKBmpInfo->bmiHeader.biWidth		=  rcBK.Width(); 
	lpBKBmpInfo->bmiHeader.biHeight		=  rcBK.Height();
	lpBKBmpInfo->bmiHeader.biPlanes		=  1; 
	lpBKBmpInfo->bmiHeader.biBitCount	=  BPPBIT;

	//#if( ALPHARES_BPP==24 )
	//	lpBmpInfo->bmiHeader.biCompression	=  BI_RGB;  
	//#else
	lpBKBmpInfo->bmiHeader.biCompression	=  BI_BITFIELDS;  
	*(DWORD *)(&lpBKBmpInfo->bmiColors[0]) = 0xF800;
	*(DWORD *)(&lpBKBmpInfo->bmiColors[1]) = 0x07E0;
	*(DWORD *)(&lpBKBmpInfo->bmiColors[2]) = 0x001F;
	//#endif

	BYTE* pDBbitPixels =NULL;
	HBITMAP hBKDBBmp = CreateDIBSection( m_DCMem.m_hDC,(BITMAPINFO*)lpBKBmpInfo,DIB_RGB_COLORS,(void**)&pDBbitPixels,NULL,0 );
	HBITMAP hOldDBBmp = (HBITMAP)SelectObject( memDIBDC.m_hDC, hBKDBBmp); 
	BitBlt( memDIBDC.m_hDC,0,0,rcBK.Width(),rcBK.Height(),pDCBK->m_hDC,0,0,SRCCOPY); 
	

	int nBKSize = (rcBK.Width()*2)*rcBK.Height();

	DisplayLevel( pDBbitPixels, LEV_LOW,memDIBDC.m_hDC );
	DisplayLevel( pDBbitPixels, LEV_MID,memDIBDC.m_hDC );
	DisplayLevel( pDBbitPixels, LEV_TOP,memDIBDC.m_hDC );

	CControl*	pCtrl = NULL;
	for ( int i=0;i<m_vecControls.size();i++ )
	{
		pCtrl = m_vecControls[i];
		pCtrl->Paint( &memDIBDC );
	}
	
	if ( bShowToFont )
	{
		pDCFront->BitBlt( m_rcRect.left,m_rcRect.top,m_rcRect.Width(),m_rcRect.Height(),memDIBDC.m_hDC,0,0,SRCCOPY );
	}

	memDIBDC.SelectBitmap( hOldDBBmp );
	memDIBDC.DeleteDC();
	DeleteObject( hBKDBBmp );

	GlobalUnlock(  hBKDBInfo );
	GlobalFree( hBKDBInfo );

}